<script>
function toggle(nombre){
	var checks = document.getElementsByTagName('input');
	var permisos = Array();
	for ( var input in checks) {
		if( checks[input].type != 'checkbox'){
			continue;
		}
		if( checks[input].id.indexOf("Group"+nombre+"/") === 0 ){
			permisos.push(checks[input].id);
		}
	}

	if( permisos.length > 0 ){
		var contador = 0;
		for(i in permisos){
			contador += document.getElementById(permisos[i]).checked ? 1 : -1;
		}
		var nuevoValor = !(contador > 0);
		for(i in permisos){
			document.getElementById(permisos[i]).checked = nuevoValor;
		}
	}
}
</script>
<div class="permisos index">
	<h2>Permisos</h2>
	<table cellpadding="0" cellspacing="0">
	<tr>
			<th>Controlador</th>
			<?php echo $this->Form->create();?>
			<?php foreach( $permisos as $permiso => $label ): ?>
			<th class="actions"><?=$label?></th>
			<?php endforeach; ?>
	</tr>
	<?php
	$i = 0;
	foreach($controladores as $controlador => $permisosControllador):
		$class = null;
		if ($i++ % 2 == 0) {
			$class = ' class="altrow"';
		}
	?>
	<tr<?php echo $class;?>>
		<td><b><a href="#" onclick="toggle('<?=$controlador?>');return false;" style="text-decoration: none"><?=$controlador?></a></b></td>
		<?php foreach( $permisos as $permiso => $label ): ?>
			<td>
			<?php
			echo $this->Form->checkbox("$controlador/$permiso", array('checked'=>$permisosControllador[$permiso]? "checked":"", 'value' => 1)); 
			?>
			</td>
		<?php endforeach; ?>
	</tr>
	<?php endforeach;?>
	</table>
	<?php
	echo $this->Form->hidden('group_id', array('value' => $group_id));
	$options = array(
	    'label' => 'Guardar',
	    'value' => 'guardar',
	    'div' => array(
	        'class' => 'glass-pill',
	    ));
	echo $this->Form->end($options);
	?>
</div>
<div class="actions">
	<h3><?php __('Actions'); ?></h3>
	<ul>
		<li><?php echo $this->Html->link(__('List Groups', true), array('controller' => 'groups', 'action' => 'index'));?></li>
		<li><?php echo $this->Html->link(__('List Users', true), array('controller' => 'users', 'action' => 'index')); ?> </li>
	</ul>
</div>